package com.yowasa.xiuxian.engine.callback.activity;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yowasa.xiuxian.annoations.NOTIFY;
import com.yowasa.xiuxian.engine.callback.CmdCallBackHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.mys.ClickMsgComponent;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuxianRedPacket;
import com.yowasa.xiuxian.mapper.XiuxianRedPacketMapper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@NOTIFY(tag = "抢红包")
public class SnatchPacketHandler extends CmdCallBackHandler {
    public static final Logger logger = LoggerFactory.getLogger(SnatchPacketHandler.class);
    @Autowired
    private MessageHelper messageHelper;

    @Autowired
    private UserHelper userHelper;

    @Autowired
    private XiuxianRedPacketMapper xiuxianRedPacketMapper;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    synchronized public void execute(ClickMsgComponent message) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setRoom_id(message.getRoom_id());
        sendMessage.setVilla_id(message.getVilla_id());
        JSONObject jsonObject = JSONUtil.parseObj(message.getExtra());
        String uid = message.getUid();
        sendMessage.setFrom_user_id(uid);
        UserBO userBO = userHelper.checkOpUser(sendMessage);

        String text = jsonObject.getStr("text");
        logger.info("红包码：{}",text);
        //先判断有没有抢过

        List<XiuxianRedPacket> packetList = xiuxianRedPacketMapper.selectList(new LambdaQueryWrapper<XiuxianRedPacket>()
                .eq(XiuxianRedPacket::getType, 0)
                .eq(XiuxianRedPacket::getPacketId, text)
                .eq(XiuxianRedPacket::getUserId, uid));
        logger.info("查询有没有抢过：{}",packetList);
        if(packetList.size() != 0){
            messageHelper.replyMys(sendMessage, "你已经抢过啦，不可以再抢了哦");
            return;
        }

        XiuxianRedPacket one = xiuxianRedPacketMapper.selectOne(new LambdaQueryWrapper<XiuxianRedPacket>()
                .eq(XiuxianRedPacket::getType, 0)
                .eq(XiuxianRedPacket::getPacketId, text)
                .isNull(XiuxianRedPacket::getUserId)
                .last("limit 1 for update"));

        if (ObjectUtil.isNotNull(one)) {
            one.setUserId(userBO.getUserId());
            one.setCreateTime(new Date());

            messageHelper.replyMys(sendMessage, userBO.getName()+"抢到了红包，获得了" + one.getGold() + "摩拉");

            xiuxianRedPacketMapper.updateById(one);
            userBO.getUserConfig().setGold(userBO.getUserConfig().getGold() + one.getGold());
            userHelper.saveUserInfo(userBO);

        }else {
            messageHelper.replyMys(sendMessage, "来晚啦，红包被抢完啦");
        }

    }


    public String desc() {
        return "抢红包";
    }


}
